package com.ludomancy.spaceopera;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import com.ludomancy.gamelib.Accumulator;
import com.ludomancy.gamelib.Randomizer;
import com.ludomancy.gamelib.Utils;
import com.ludomancy.gamelib.Widget;

public class SpaceWidget extends Widget
{
	public Accumulator shake;
	
	public SpaceWidget(Widget parent, Rectangle area)
	{
		super(parent, area);
	}
	
	public void startShake()
	{
		shake = new Accumulator(80);
	}

	public void update()
	{
		Game.game.update();
	}
	
	public void paint(Graphics g)
	{
		if(Game.game.paused)
		{
			g.setColor(Color.GREEN);
			g.setFont(GameTheme.theme.bigFont);
			Utils.drawString(g, "Paused", getWidth()/2, getHeight()/2, Utils.ANCHOR_VCENTER|Utils.ANCHOR_HCENTER);

			return;
		}

		int shakeAmount = 0;
		if(shake != null && shake.isRunning())
		{
			shakeAmount = Randomizer.getRandom(-1,1);
			g.translate(0, shakeAmount);
		}

		Game.game.currentWorld.paint(g, Game.game.defaultProjector);
		// paint interactable entity
		if(Game.game.player.canInteract())
		{
			Point pos = Game.game.player.interactableEntity.pos;
			pos = Game.game.defaultProjector.worldToScreen(pos);
			int w = Game.game.player.interactableEntity.screenSize(Game.game.defaultProjector).x;
			int h = Game.game.player.interactableEntity.screenSize(Game.game.defaultProjector).y;
			paintOutline(g, Game.game.player.getInteractionVerb(), pos.x - w/2, pos.y - h/2, w, h);
		}
		
		if(shake != null && shake.isRunning())
		{
			g.translate(0, -shakeAmount);
			shake.update();
		}
	}
	
	public static void paintOutline(Graphics g, String text, int x, int y, int w, int h)
	{
		g.setColor(Color.GREEN);
		Utils.drawTargetOutline(g, x - 3, y - 3, w + 4, h + 4);
		g.setFont(GameTheme.theme.ingame);
		Utils.drawString(g, text, x + w + 4, y + h/2 - 2 , Utils.ANCHOR_BOTTOM|Utils.ANCHOR_VCENTER);
	}
	
	public void mouseMoved(Point point)
	{
		Point newPos = Game.game.defaultProjector.screenToWorld(point);
		Game.game.player.setTarget(newPos);
	}
	
	public void mouseDown(Point pos)
	{
		Game.game.player.activated = true;
		mouseMoved(pos);
	}

	public void mouseUp(Point pos)
	{
		Game.game.player.activated = false;
	}

	public void onMouseLeave()
	{
		Game.game.player.activated = false;
	}
}
